package com.bw.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.house.common.ResponseResult;
import com.bw.house.common.auth.UserInfo;
import com.bw.house.common.utils.MD5Util;
import com.bw.house.entity.TbHouse;
import com.bw.house.entity.TbUser;


import com.bw.house.mapper.TbUserMapper;
import com.bw.house.service.ITbUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 邢晋宁
 * @since 2020-09-04
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {

    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 用户登录
     **/

    @Override
    public ResponseResult login(TbUser user, HttpServletResponse response) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            String pwd = MD5Util.md5Encrypt32Lower(user.getLoginPwd());
            queryWrapper.eq("LOGIN_NAME",user.getLoginName());
            queryWrapper.eq("LOGIN_PWD", pwd);

            List<TbUser> list = userMapper.selectList(queryWrapper);
            if(list.size()==0 || list==null){//登录不正确
                result.setSuccess(false);
                result.setMessage("登陆失败，用户名或密码不正确！！！");
                return result;
            }

            TbUser sysUser = list.get(0);

            //随机生成一个token，存储到浏览器的cookie中，同时token存储到redis当中【cookie+redis实现了登录】
            UserInfo userInfo = new UserInfo(sysUser.getId(),sysUser.getLoginName());
            String token = UUID.randomUUID().toString();
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(1800);//超时时间  设置最长时间--秒
            cookie.setPath("/");
            response.addCookie(cookie);

            redisTemplate.opsForValue().set(token,userInfo);
            redisTemplate.expire(token,30, TimeUnit.MINUTES);
            Map map = new HashMap();
            map.put("user",sysUser);
            map.put("token",token);

            result.setMessage("登陆成功！！！");
            result.setResult(map);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("登陆失败！！！"+e.toString());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 用户列表展示
     **/
    @Override
    public ResponseResult listAll(int currentPage, int pageSize, TbUser user) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PageHelper.startPage(currentPage,pageSize);
            QueryWrapper queryWrapper = new QueryWrapper();
            if(user.getShowName()!=null){//用户名称
                queryWrapper.like("NAME","%"+user.getShowName()+"%");
            }
            if(user.getStatus()!=null){//用户角色
                queryWrapper.eq("STATUS",user.getStatus());
            }
            if(user.getMobile()!=null){//用户手机号
                queryWrapper.eq("STATUS",user.getMobile());
            }

            List<TbUser> userList = userMapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(userList);

            result.setResult(pageInfo);
            result.setMessage("查询成功！！！");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败！！！"+e.toString());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 注册房东
     **/
    @Override
    public ResponseResult saveUser(TbUser user) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            user.setLoginPwd(MD5Util.md5Encrypt32Lower(MD5Util.md5Encrypt32Lower(user.getLoginPwd())));
            user.setCreateTime(new Date());
            userMapper.insert(user);
            result.setMessage("注册成功！！！");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("注册失败！！！"+e.toString());
            e.printStackTrace();
        }
        return result;
    }
}
